"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const maishu_node_mvc_1 = require("maishu-node-mvc");
const common_1 = require("../common");
const common_2 = require("common");
const common_3 = require("../common");
const maishu_chitu_service_1 = require("maishu-chitu-service");
const gateway_1 = require("maishu-node-auth/gateway");
const common_4 = require("common");
const errors_1 = require("../errors");
let UserController = class UserController extends maishu_node_mvc_1.Controller {
    async list(dc, { args }) {
        let items = await common_2.DataHelper.list(dc.user, { selectArguments: args });
        return items;
    }
    async login({ code }, dc, appId) {
        if (!appId)
            throw errors_1.errors.cannotGetApplicationId();
        if (!code)
            throw errors_1.errors.routeDataFieldNull("code");
        let wxSetting = await dc.settings.findOne({ id: appId });
        if (!wxSetting)
            throw errors_1.errors.wxSettingNull();
        if (!wxSetting.AppId)
            throw errors_1.errors.objectFieldNull("AppId", "wxSetting");
        let wx = common_3.createWeiXinSDK({
            appId: wxSetting.AppId, appKey: wxSetting.AppSecret,
            partnerId: wxSetting.PartnerId, partnerKey: wxSetting.PartnerKey,
            miniAppId: wxSetting.MiniProgramAppId, miniAppKey: wxSetting.MiniProgramSecret,
        });
        let r = await wx.access_token(code);
        let user = await dc.user.findOne({ openId: r.openid, applicationId: appId });
        if (user == null) {
            user = {
                id: maishu_chitu_service_1.guid(), openId: r.openid,
                createDateTime: new Date(Date.now()), applicationId: appId,
            };
            await dc.user.insert(user);
        }
        let loginResult = { userId: user.id };
        return new maishu_node_mvc_1.ContentResult(JSON.stringify(loginResult), "application/json", gateway_1.statusCodes.login);
    }
};
__decorate([
    maishu_node_mvc_1.action(),
    __param(0, common_1.weixinDataContext), __param(1, maishu_node_mvc_1.routeData)
], UserController.prototype, "list", null);
__decorate([
    maishu_node_mvc_1.action(),
    __param(0, maishu_node_mvc_1.routeData), __param(1, common_1.weixinDataContext), __param(2, common_4.currentAppId)
], UserController.prototype, "login", null);
UserController = __decorate([
    maishu_node_mvc_1.controller("user")
], UserController);
exports.UserController = UserController;
